import FView from "./FView";
import { GameLayer } from "../Enums/GameLayer";
import { Application } from "./Application";
import { FMediator } from "./FMediator";
import { EventManager } from "../Managers/EventManager";
import { GameEvent } from "../Event/GameEvent";
import { FEvent } from "../Event/FEvent";
import { AudioManager } from "../Managers/AudioManager";
import { stageWidth } from "../Utility/dx/stageWidth";
import { stageHeight } from "../Utility/dx/stageHeight";
import { ResouceManager } from "../Managers/ResouceManager";
import { Client } from "../Network/Socket/Client";
import { FProxy } from "./FProxy";


const { ccclass, property, menu } = cc._decorator;
@ccclass

@menu("Framework/基础组件/FScene")
export default class FScene extends FView {
    private static layerNames: string[] = ["Background", "Content", "UI", "PopupMask", "Popup", "WindowMask", "Window"];
    //--------------------------------------------------------------------------------------------------------------------
    // 编辑器相关
    //--------------------------------------------------------------------------------------------------------------------
    @property({ serializable: true })
    private _creatLayer: boolean = false;
    @property({ serializable: true })
    public get creatLayer(): boolean { return this._creatLayer; }
    public set creatLayer(value: boolean) {
        this._creatLayer = value;
        if (value) this.createLayers();
    }
    //--------------------------------------------------------------------------------------------------------------------
    // 
    //--------------------------------------------------------------------------------------------------------------------

    private layers: cc.Node[] = [];
    protected mediator: FMediator;
    protected proxy: FProxy;
    protected client: Client;


    public getProxy(): FProxy {
        return this.proxy;
    }

    public setProxy(p: FProxy) {
        this.proxy = p;
    }

    public onLoad(): void {
        Application.init();
        Application.setCurrentScene(this);
        this.createLayers();
        EventManager.addEvent(this, GameEvent.ON_PLAY_EFFECT, this.onPlayEffectEvent);
        // this.proxy && this.proxy.initialize();
    }

    private onPlayEffectEvent(evt: FEvent): void {
        AudioManager.PlayEffect(evt.data);
    }

    private createLayers(): void {
        this.layers.length = 0;
        let scene = cc.director.getScene()
        for (let i = 0; i < FScene.layerNames.length; i++) {
            let layer = this.node.getChildByName(FScene.layerNames[i]);
            if (layer == null) {
                layer = new cc.Node(FScene.layerNames[i]);
                this.node.addChild(layer);
                layer.setContentSize(stageWidth(), stageHeight());
            }
            this.layers.push(layer);
        }
    }

    public getLayer(layer: GameLayer): cc.Node {
        return this.layers[layer];
    }
    public onDisable(): void {
        this.mediator && this.mediator.dispose();
        this.proxy && this.proxy.dispose();
        super.onDisable();
        this.mediator = null;
    }
    public onDestroy() {
        Application.onCurrentSceneDestroy();
    }

    public onGetSceneData(data: any): void { }
}
